import pygame
import math

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
ORANGE = (255, 165, 0)
RED = (255, 0, 0)

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("篮球游戏")

# 篮球运动员类
class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.speed = 5

    def move_left(self):
        if self.x > 0:
            self.x -= self.speed

    def move_right(self):
        if self.x < WIDTH - 50:
            self.x += self.speed

    def draw(self):
        pygame.draw.rect(screen, BLUE, (self.x, self.y, 50, 100))

# 篮球类
class Basketball:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = 20
        self.vx = 0
        self.vy = 0
        self.gravity = 0.5

    def shoot(self, power, angle):
        self.vx = power * math.cos(angle)
        self.vy = -power * math.sin(angle)

    def move(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += self.gravity
        # 边界检测
        if self.x - self.radius < 0 or self.x + self.radius > WIDTH:
            self.vx = -self.vx
        if self.y - self.radius < 0:
            self.vy = -self.vy
        if self.y + self.radius > HEIGHT:
            self.vy = 0
            self.y = HEIGHT - self.radius

    def draw(self):
        pygame.draw.circle(screen, ORANGE, (int(self.x), int(self.y)), self.radius)

# 篮筐类
class Hoop:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 100
        self.height = 20

    def draw(self):
        pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))

    def is_scoring(self, ball):
        ball_bottom = ball.y + ball.radius
        ball_left = ball.x - ball.radius
        ball_right = ball.x + ball.radius
        return (ball_bottom >= self.y and
                ball_left >= self.x and
                ball_right <= self.x + self.width)

# 创建游戏元素
player = Player(375, 500)
ball = Basketball(player.x + 25, player.y)
hoop = Hoop(350, 100)
score = 0

# 游戏主循环
clock = pygame.time.Clock()
shooting = False
power = 0
angle = 0
running = True
while running:
    screen.fill(WHITE)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.move_left()
                ball.x = player.x + 25
                ball.y = player.y
            elif event.key == pygame.K_RIGHT:
                player.move_right()
                ball.x = player.x + 25
                ball.y = player.y
            elif event.key == pygame.K_UP:
                shooting = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                shooting = False
                ball.shoot(power, angle)
                power = 0

    if shooting:
        power += 0.1
        angle = math.atan2(100 - ball.y, 375 - ball.x)

    # 篮球移动
    ball.move()

    # 检查是否得分
    if hoop.is_scoring(ball):
        score += 1
        ball.x = player.x + 25
        ball.y = player.y
        ball.vx = 0
        ball.vy = 0

    # 绘制游戏元素
    player.draw()
    ball.draw()
    hoop.draw()

    # 显示得分
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"得分: {score}", True, BLUE)
    screen.blit(score_text, (10, 10))

    # 更新显示
    pygame.display.flip()
    clock.tick(60)

# 退出游戏
pygame.quit()